package cuiyt.juc.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author cyt
 * @create 2020-04-30 0:03
 */
public class test8 {
    public static void main(String[] args) throws InterruptedException {
        phone8 phone1 = new phone8();
        phone8 phone8 = new phone8();
        new Thread(() -> {
            try {
                phone1.sendMes();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> {
            //phone8.call();
            try {
                phone1.sendMes();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();
    }
}



class phone8 {
    // synchronized 会获得方法调用者的锁 ， 并且在方法结束之前一直持有。
    public static synchronized void sendMes() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(4);
        System.out.println("发短信");
    }
    public synchronized void call() {
        System.out.println("打电话");
    }

}
